package com.qf.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.device.entity.DeviceType;
import com.qf.device.mapper.DeviceTypeMapper;
import com.qf.device.service.DeviceTypeService;
import com.qf.dto.PageDto;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备类型表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-26
 */
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements DeviceTypeService {

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Override
    public PageResult<DeviceType> pageInfo(PageDto<DeviceType> pageDto) {
        Long industryId = null;
        int deviceType = 0;
        String name = null;
        //条件判断
        if (!StringUtils.isEmpty(pageDto.getWhere().getIndustryId())){
            industryId = pageDto.getWhere().getIndustryId();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getDeviceType())){
            deviceType = pageDto.getWhere().getDeviceType();
        }
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            name = pageDto.getWhere().getName();
        }
        //获取所有的deviceType列表
        List<DeviceType> deviceTypeList = deviceTypeMapper.findAll(pageDto, industryId, deviceType, name);
        //查询总条数
        Long count = deviceTypeMapper.count(pageDto, industryId, deviceType, name);
        //计算总页数
        Long pages = (count + pageDto.getSize() - 1) / pageDto.getSize();
        //封装数据
        return new PageResult<>(count,pages,deviceTypeList);
    }

    @Override
    public Result add(DeviceType deviceType) {
        if(StringUtils.isEmpty(deviceType)) {
            deviceType.setCreateTime(new Date());
            deviceType.setUpdateTime(new Date());
            deviceType.setDeleted(0);
            int insert = deviceTypeMapper.insert(deviceType);
            if (insert == 1) {
                return new Result(0, "添加成功！");
            }else{
                return new Result(1, "添加失败！非法数据");
            }
        }else{
            return new Result(1, "添加失败！非法数据");
        }
    }
}
